package com.example.springboot.Dao;

import com.example.springboot.entity.User;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;

import java.util.List;
@RepositoryRestResource(path = "person")
public interface UserRepository extends JpaRepository<User,Integer> {
    User findByUid(Integer uid);
    // 通过账户查找
    User findByAccount(Integer account);
    // 通过name模糊查找
    List<User> findByNameLike(String name);
    //根据name查询前十条记录
    List<User> findTop10ByName(String name);

    int deleteByAccount(Integer account);

    //添加@Query注解，使用hql语句查询
    @Query("select u from User u where u.account=?1")
    User findByAccountHql(Integer account);

    //使用命名参数，使用hql语句根据名称匹配查询参数
    @RestResource(path = "nameStartsWith",rel = "nameStartsWith")
    @Query("select u from User u where u.name= :name")
    List<User> findByName(@Param("name") String name);

    //更新查询，Spring Data Jpa 支持使用@modifying和@Query注解组合来事件更新查询
    @Modifying
    @Query("update User u set u.name=?1")
    int setName(String name);

   /* //排序查询
    List<User> findByNameLike(String name, Sort sort);
    //分页查询
    List<User> findByNameLike(String name, Pageable pageable);*/
}
